<?php
App::uses('AppController', 'Controller');
/**
 * Consigners Controller
 *
 * @property Consigner $Consigner
 */
class ConsignersController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Consigner->recursive = 0;
		$this->set('consigners', $this->paginate());
	}
	
	public function indexdata() {	

        $this->autoRender = false;  
		$output = $this->Consigner->GetData();	
		echo json_encode($output);
		
	}
	

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Consigner->exists($id)) {
			throw new NotFoundException(__('Invalid consigner'));
		}
		$options = array('conditions' => array('Consigner.' . $this->Consigner->primaryKey => $id));
		$consigner = $this->Consigner->find('first', $options);
		$this->set('consigner', $consigner);
		

		for($c=0;$c<count($consigner['Consignment']);$c++)
		{
			$items = "";
			$id = $consigner['Consignment'][$c]['id'];
			$ConsignmentItem = $this->loadModel('ConsignmentItem');
			$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $id
					) 
			);
			$consignmentItem = $this->ConsignmentItem->find ( "all",$options );
			$itemlist = array();
			foreach ( $consignmentItem as $v ) {$itemlist[]=$v["Item"];	}	

			
			foreach ( $itemlist as $item ) {		
				$items .= "<a href='items/view/".$item['id']."'>".$item['name']."</a>,";
			}
			
			$itemsA[] = rtrim($items,",");
			
		}
		
		$this->set('items', $itemsA);
		
		
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Consigner->create();
			$result = $this->Consigner->save($this->request->data);
			
			if ($result) {
            // if ajax request, then back to json data
            if ($this->request->is ( "ajax" )) {
                header ( "Pragma: no-cache" );
                header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
                header ( 'Content-Type: text/x-json' );
                header ( 'Content-type: application/json' );
                $consignerlist = array ();
                $status=false;

                $consigner_all = $this->Consigner->find ( "all" );
						
				if (! empty ( $consigner_all ))
				{
					foreach ( $consigner_all as $v ) {$consignerlist[]=$v["Consigner"];}						
				}
				
                $status=true;
				
			   $msg = array (
					__ ( 'The Consigner has been saved' ),
					__ ( 'The Consigner could not be saved. Please, try again.' )
				);
                $data = array (
                    "status" => $status,
                    "message" => $status ? $msg [0] : $msg [1],
                    "consignerlist" => $consignerlist
                ); 
				
                echo json_encode ( $data );
                die ();
				
            } elseif ($result) {
                $this->Session->setFlash ( $msg [0] );
                $this->redirect ( array (
                    'action' => 'index'
                ) );
            } else {
                $this->Session->setFlash ( $msg [1] );
            }
			
			$this->Session->setFlash(__('The consigner has been saved'));			
			$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The consigner could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Consigner->exists($id)) {
			throw new NotFoundException(__('Invalid consigner'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Consigner->save($this->request->data)) {
				$this->Session->setFlash(__('The consigner has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The consigner could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Consigner.' . $this->Consigner->primaryKey => $id));
			$this->request->data = $this->Consigner->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Consigner->id = $id;
		if (!$this->Consigner->exists()) {
			throw new NotFoundException(__('Invalid consigner'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Consigner->delete()) {
			$this->Session->setFlash(__('Consigner deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Consigner was not deleted'));
		$this->redirect(array('action' => 'index'));
	}

    public function search() {
        //$this->set('results',$this->Customer->search($this->data['Customer']['q']));

        $keyword=$this->request->data['Consigner']['keyword']; //get keyword from querystring//
        //used simpme or condition with singe value checking
        //replace ModelName with actual name of your Appmodel

        //$this->Session->setFlash($keyword);

        $conditions=array('OR'=>array(
            'Consigner.id LIKE'=> $keyword,
            'Consigner.name LIKE'=> $keyword,
            'Consigner.phone LIKE'=> $keyword));

        $this->set('isFound', $this->Consigner->find('count',array(
                    'conditions'=>$conditions)
            )
        );

        $this->set('list', $this->Consigner->find('all',array(
                    'conditions'=>$conditions)
            )
        );
    }

}
